package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.medical.common.SecurityHelper;
import com.jzr.medical.db1.mapper.TDeviceMapper;
import com.jzr.medical.db1.mapper.TPatientCareLabelRefMapper;
import com.jzr.medical.db1.mapper.TPatientExtraMapper;
import com.jzr.medical.db1.mapper.TPatientMapper;
import com.jzr.medical.db1.model.TDevice;
import com.jzr.medical.db1.model.TPatient;
import com.jzr.medical.db1.model.TPatientCareLabelRef;
import com.jzr.medical.db1.model.TPatientExtra;
import com.jzr.medical.db1.service.PatientService;
import com.jzr.medical.db1.vo.BedPatientVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<TPatientMapper,TPatient> implements PatientService {
    @Autowired
    private TPatientCareLabelRefMapper patientCareLabelRefMapper;

    @Autowired
    private TPatientExtraMapper patientExtraMapper;

    @Autowired
    private TDeviceMapper deviceMapper;

    @Override
    public List<BedPatientVo> selectBedPatientVo(String deptCode){
       return baseMapper.selectBedPatientVo(deptCode);
    }


    @Override
    public boolean saveCareLabelList(Integer patientId,List<Integer> careLabelIds){
        if(patientId == null || careLabelIds == null || careLabelIds.size() <=0 ){
            return false;
        }
        Integer userId = SecurityHelper.getUserId();

        //删除未有选定的
        patientCareLabelRefMapper.delete(new QueryWrapper<TPatientCareLabelRef>()
                .eq(TPatientCareLabelRef.PatientId_,patientId)
                .notIn(TPatientCareLabelRef.CareLabelId_,careLabelIds));

        //插入不存在的
        for(Integer careLabelId : careLabelIds){
            Integer count = patientCareLabelRefMapper.selectCount(new QueryWrapper<TPatientCareLabelRef>()
                    .eq(TPatientCareLabelRef.PatientId_,patientId)
                    .eq(TPatientCareLabelRef.CareLabelId_,careLabelId));
            if(count == null || count.intValue()<=0) {
                TPatientCareLabelRef labelRef = new TPatientCareLabelRef();
                labelRef.setPatientId(patientId);
                labelRef.setCareLabelId(careLabelId);
                labelRef.setUpdateBy(userId);
                labelRef.setUpdateTime(LocalDateTime.now());
                patientCareLabelRefMapper.insert(labelRef);
            }
        }
        return true;
    }

    @Override
    public boolean saveExtra(TPatientExtra patientExtra){
        Integer userId = SecurityHelper.getUserId();

        Integer patientId = patientExtra.getPatientId();
        if(patientId == null){
            return false;
        }

        Integer count = patientExtraMapper.selectCount(new QueryWrapper<TPatientExtra>()
                .eq(TPatientExtra.PatientId_,patientId));
        if(count==null || count.intValue()<=0){
            patientExtra.setUpdateBy(userId);
            patientExtra.setUpdateTime(LocalDateTime.now());
            patientExtraMapper.insert(patientExtra);
        }else{
            patientExtra.setUpdateBy(userId);
            patientExtra.setUpdateTime(LocalDateTime.now());
            patientExtraMapper.updateById(patientExtra);
        }
        return true;
    }

    @Override
    public TDevice getBedDeviceByPatientId(Integer patientId) {
        QueryWrapper<TDevice> qw = new QueryWrapper<>();
        qw.apply("register_bed in (select id from t_bed where patient_id={0})",patientId);
        return deviceMapper.selectOne(qw);
    }
}
